package com.xinzhilibrary.service.impl;

import com.xinzhilibrary.dao.IGoodsInfoDao;
import com.xinzhilibrary.model.GoodsInfo;
import com.xinzhilibrary.model.OrderInfo;
import com.xinzhilibrary.model.Result;
import com.xinzhilibrary.service.GoodsInfoService;
import com.xinzhilibrary.util.CheckJWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {
    @Autowired
    private IGoodsInfoDao iGoodsInfoDao;
    CheckJWT checkJWT = new CheckJWT();

    @Override
    public Result selectAllGoods(int startNum, int lineNum, String goodsName,int goodsType, String token) {
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            String newName = "%" + goodsName + "%";
            ArrayList<GoodsInfo> goodsInfos = iGoodsInfoDao.selectAllGoods(startNum, lineNum, newName,goodsType);
            return Result.success(goodsInfos);
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }

    @Override
    public Result insertOrder(OrderInfo orderInfo, String token) {
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            OrderInfo orderInfo1 = iGoodsInfoDao.checkGoodsInfo(orderInfo.getOrderGoodsName(), orderInfo.getOrderGoodsUserId());
            if (orderInfo1 != null) {
                int orderGoodsNumber = orderInfo1.getOrderGoodsNumber();
                orderGoodsNumber += orderInfo.getOrderGoodsNumber();
                double orderValue = orderInfo1.getOrderValue();
                orderValue += orderInfo.getOrderValue();
                int i1 = iGoodsInfoDao.updateOrderInfo(orderInfo.getOrderGoodsName(), orderGoodsNumber, orderValue);
                if (i1 > 0) {
                    return Result.success("添加购物车成功");
                } else {
                    return Result.error("添加购物车失败");
                }
            } else {
                int i = iGoodsInfoDao.insertOrder(orderInfo);
                if (i > 0) {
                    return Result.success("新增购物车商品订单成功");
                } else {
                    return Result.error("新增购物车商品订单失败");
                }
            }
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }

    @Override
    public Result selectOrderByUID(int orderGoodsUserId, String token) {//查看用户购物车中的订单
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            ArrayList<OrderInfo> orderInfos = iGoodsInfoDao.selectOrder(orderGoodsUserId);
            return Result.success(orderInfos);
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }

    @Override
    public Result selectAllOrder(int orderGoodsUserId, String token) {//查看用户历史订单（包含当下购物车中的内容）
        boolean b = checkJWT.checkJwt(token);
        if (b) {
            ArrayList<OrderInfo> orderInfos = iGoodsInfoDao.selectAllOrder(orderGoodsUserId);
            return Result.success(orderInfos);
        } else {
            return Result.error("身份验证失效，请重新登录");
        }
    }
}
